package com.github.ruangnazi.sharing.ui.home

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.github.ruangnazi.sharing.databinding.FragmentHomeBinding
import com.github.ruangnazi.sharing.master.Gundam
import com.github.ruangnazi.sharing.master.ShardingSystem
import java.util.*
import kotlin.collections.ArrayList

class HomeFragment : Fragment() {

    private lateinit var homeViewModel: HomeViewModel
    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    private lateinit var startActivitylaunch: ActivityResultLauncher<Intent>

    private lateinit var sharIntets: MutableList<Intent>


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        startActivitylaunch = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            //获取到BActivity回传的数据
            val resultCode = it.resultCode
            val data = it.data

            Log.e("lunch back",resultCode.toString() + " " + data.toString())
            activeShareIntent()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

//        val textView: TextView = binding.textHome
//        homeViewModel.text.observe(viewLifecycleOwner, Observer {
//            textView.text = it
//        })

        val tvDeleteText: TextView = binding.tvdeletetext
        val mRecyclerView: RecyclerView = binding.recycler
        var title: EditText = binding.editTextTextMultiLine
        var buttonSend: Button = binding.buttonSend


//        val tvDeleteText = activity?.findViewById<TextView>(R.id.tv_delete_text);
//        val mRecyclerView = activity?.findViewById<RecyclerView>(R.id.recycler);

        Log.e("home activity",activity.toString())
        val gn = Gundam(this@HomeFragment)
        context?.let {
            if ( tvDeleteText != null &&mRecyclerView != null) {
                gn.initGrid(it, savedInstanceState, tvDeleteText, mRecyclerView)
                buttonSend.setOnClickListener {
                    var ssm = ShardingSystem(this@HomeFragment)
                    val imageUris: ArrayList<Uri> = ArrayList()
                    val imagePaths: ArrayList<String> = ArrayList()

                    for (media in gn.callbackMedias){
//                    var uri = Uri.parse(MediaStore.Images.Media.insertImage(mActivity.getContentResolver(), bitmap, "IMG" + Calendar.getInstance().getTime(), null));
                        var uri = Uri.parse(media.path)
                        imageUris.add(uri)

                        imagePaths.add(media.androidQToPath)
                    }
                    sharIntets  = ssm.SharingPhoto(title.text.toString(),imageUris,imagePaths)
                    activeShareIntent()
                    Log.e("send","send start "+ title.text.toString())
                }

            }else{
                Log.e("home","frgame not init")
            }
        }
        return root
    }

    fun activeShareIntent(){
        if (sharIntets.size>0){
            val inte = sharIntets.removeLast()
            startActivitylaunch.launch(inte)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}